package com.game.framework.utill;

import akka.actor.ActorRef;
import akka.pattern.Patterns;
import akka.util.Timeout;
import scala.concurrent.Await;
import scala.concurrent.Future;
import scala.concurrent.duration.Duration;

import java.util.concurrent.TimeUnit;

public class ActorUtil{

    /// ask
    public static <T> T ask(ActorRef actor, Object message) {
        try {
            Timeout timeout = new Timeout(Duration.create(50, TimeUnit.SECONDS));
            Future<Object> future = Patterns.ask(actor, message, timeout);

            return (T) Await.result(future, timeout.duration());
        } catch (Exception e) {
            throw new RuntimeException("Actor communication failed", e);
        }
    }
}
